﻿using Furion.CMS.Core.Base;
using Furion.CMS.Domain;
using Furion.LinqBuilder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    public class DataDictionaryFilter : BaseFilterParams<DataDictionary>
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTimeOffset? StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTimeOffset? EndTime { get; set; }
        /// <summary>
        /// 关键字查询
        /// </summary>
        public string Keyword { get; set; }
        /// <summary>
        /// 类别Id
        /// </summary>
        public Guid? CategoryId { get; set; }

        public override Expression<Func<DataDictionary, bool>> GetFilterExpression()
        {
            Expression<Func<DataDictionary, bool>> condition = x => true;
            if (StartTime.HasValue)
            {
                condition = condition.And(x => x.CreatedTime >= StartTime.Value.Date);
            }
            if (EndTime.HasValue)
            {
                EndTime = EndTime.Value.AddDays(1);
                condition = condition.And(x => x.CreatedTime < EndTime.Value.Date);
            }
            if (!string.IsNullOrWhiteSpace(Keyword))
            {
                condition = condition.And(x => x.DataKey.Contains(Keyword) || x.DataValue.Contains(Keyword));
            }
            if (CategoryId.HasValue)
            {
                condition = condition.And(x => x.CategoryId == CategoryId);
            }
            return condition;
        }
    }
}
